package com.yanqu.road.dao.impl.activity.climbtower;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.climbtower.UserClimbTowerTopClimbRecord;
import com.yanqu.road.entity.player.UserBaseInfo;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.*;

public class UserClimbTowerTopClimbRecordDaoImpl extends TempDataObjectDao<UserClimbTowerTopClimbRecord> {

    public static String getDataSql = "select * from `t_u_climb_tower_top_climb_record` where `activityId`=? and `userId`=? order by recordTime desc limit ?";

    public LinkedList<UserClimbTowerTopClimbRecord> getUserClimbTowerTopClimbRecordList(int activityId, long userId, int maxCount) {
        ResultSet rs = null;
        CallableStatement call = null;
        LinkedList<UserClimbTowerTopClimbRecord> result = new LinkedList<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            call.setInt(i++, maxCount);
            rs = call.executeQuery();
            while (rs.next()) {
                UserClimbTowerTopClimbRecord data = parse(rs);
                result.addLast(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String insertDataSql = "insert into `t_u_climb_tower_top_climb_record`(recordId,activityId,userId,ability,enemyUserId,enemyBaseInfo,enemyAbility," +
            "oldCellNum,newCellNum,recordTime)" +
            " values (?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(UserClimbTowerTopClimbRecord data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, data.getRecordId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getAbility().toString()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getEnemyUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getEnemyBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getEnemyAbility().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getOldCellNum()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getNewCellNum()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getRecordTime()));
        if (getDb().executeNoneQuery(insertDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected boolean updateData(UserClimbTowerTopClimbRecord data) {
        return false;
    }

    @Override
    protected UserClimbTowerTopClimbRecord parseData(ResultSet rs) throws SQLException {
        UserClimbTowerTopClimbRecord data = new UserClimbTowerTopClimbRecord();
        data.setRecordId(rs.getString("recordId"));
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setAbility(new BigInteger(rs.getString("ability")));
        data.setEnemyUserId(rs.getLong("enemyUserId"));
        data.setEnemyBaseInfo(JSONObject.parseObject(rs.getString("enemyBaseInfo"), UserBaseInfo.class));
        data.setEnemyAbility(new BigInteger(rs.getString("enemyAbility")));
        data.setOldCellNum(rs.getInt("oldCellNum"));
        data.setNewCellNum(rs.getInt("newCellNum"));
        data.setRecordTime(rs.getLong("recordTime"));
        return data;
    }
}
